﻿/**********************************************
 * 功能名称：成本细项定义
编写人：  杨莹
建立时间： 杨2012年6月19日
说明：
对成本的明细项目进行设置，包括查询、增加、删除、修改等。

 *
 * 
 * 
 **************************************************************/

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Hisun.Models;
using Hisun.Controllers;
using System.Collections.Generic;


namespace Hisun.View.CostItemsSetup
{
    public partial class CostItemsSetup : AbcPageBase
    {
        HisunDBDataContext db = new HisunDBDataContext();
        public string alert;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                EditState("Browse");
                int Totals = TotalPageAmount();



                int pages = (Totals % 10) == 0 ? (Totals / 10) : (Totals / 10 + 1);

                Label_Total.Text = "共" + pages.ToString();
                Label_CurrenPageAndTo.Text = "页，当前第1" +"页，转到";
                BindMyData(0);

            
            }
        }


        private void BindMyData(int PageIndex)
        {

            int skipAccount = PageIndex * 10;

            var MyQuery = (from a in db.TABC_DM_CostItems where a.成本细项名称.Contains(TextBox_QueryText.Text) orderby a.报表顺序码 select a).Skip(skipAccount).Take(10);
                    GridView_CostItemsList.DataSource = MyQuery;
                    GridView_CostItemsList.DataBind();

     


        }


        //翻页的功能
        //查询出当前记录信息的总条数。
        private int TotalPageAmount()
        {
            int MyQuery = 0;

            MyQuery = (from a in db.TABC_DM_CostItems where a.成本细项名称.Contains(TextBox_QueryText.Text) select a).Count();
  

            return MyQuery;
        }


        //点击按钮，执行查询
        protected void MyClick(object sender, EventArgs e)
        {


            int pageIndx = Convert.ToInt32(CurrentPage.Value);
            int Totals = TotalPageAmount();



            int pages = (Totals % 10) == 0 ? (Totals / 10) : (Totals / 10 + 1);

            Label_Total.Text = "共" + pages.ToString();

            if (typeof(Button).IsInstanceOfType(sender))
            {
                string arg = ((Button)sender).CommandArgument.ToString().ToLower();

                switch (arg)
                {
                    case "first":

                        pageIndx = 0;

                        break;
                    case "gotopage":
                        if (TextBox_PageIndex.Text == "")
                        { pageIndx = 0; }
                        else
                        {
                            int xx = Convert.ToInt32(TextBox_PageIndex.Text);
                            if (xx > pages)
                            { pageIndx = pages - 1; }

                            else
                            { pageIndx = xx - 1; }


                        }
                        break;
                }

            }
            else if (typeof(LinkButton).IsInstanceOfType(sender))
            {

                string arg = ((LinkButton)sender).CommandArgument.ToString().ToLower();

                switch (arg)
                {
                    case "prev":
                        if (pageIndx > 0)
                        {
                            pageIndx = pageIndx - 1;
                        }
                        break;
                    case "next":
                        if (pageIndx < pages - 1)
                        {
                            pageIndx = pageIndx + 1;
                        }
                        break;
                    case "last":
                        pageIndx = pages - 1;
                        break;
                    case "first":
                        pageIndx = 0;
                        break;
                }

            }

            CurrentPage.Value = pageIndx.ToString();  //对保存页码状态信息的隐藏字段进行赋值。保存当前页码。
            Label_CurrenPageAndTo.Text = "页，当前第" + (pageIndx + 1).ToString() + " 页，转到";

            BindMyData(pageIndx);
            EditState("Browse");

            if (GridView_CostItemsList.Rows.Count == 0)
            { Panel_GridList.Visible = false; }
            else
            { Panel_GridList.Visible = true; }

        }

        protected void EditState(string MyState)
        {
            switch (MyState)
            {
                case  "Add":
                    {
                        Panel_GridList.Visible = false;
                        Panel_Edit.Visible = true;
                        TextBox_Id.ReadOnly = false;
                        EditFlag.Value = "Add";
                        break;
                    };
                case "Edit":
                    {
                        Panel_GridList.Visible = false;
                        Panel_Edit.Visible = true;
                        TextBox_Id.ReadOnly = true;
                        EditFlag.Value = "Edit";

                        break;
                    };
                case "Browse":
                    {
                        Panel_GridList.Visible = true;
                        Panel_Edit.Visible = false;
                        break;
                    };
            }
        
        }

        protected void Button_Add_Click(object sender, EventArgs e)
        {
            EditState("Add");
         
        }

        protected void SortCodeRefresh(int NewCode,int OldCode)
        {
            //在插入点及以后的报表顺序码都加1.
            if (NewCode < OldCode)
            {
                var SortCodeList = from a in db.TABC_DM_CostItems where a.报表顺序码 >= NewCode && a.报表顺序码 < OldCode select a;

                foreach (var SortcodeRow in SortCodeList)
                {

                    SortcodeRow.报表顺序码++;
                    db.SubmitChanges();

                }
            }
            if (NewCode > OldCode)
            {

                var SortCodeList = from a in db.TABC_DM_CostItems where a.报表顺序码 > OldCode && a.报表顺序码 <= NewCode select a;

                foreach (var SortcodeRow in SortCodeList)
                {

                    SortcodeRow.报表顺序码--;
                    db.SubmitChanges();

                }
            }


        
        
        }

        protected void Button_Save_Click(object sender, EventArgs e)
        {
            var MaxSortCode = (from a in db.TABC_DM_CostItems select a.报表顺序码).Max();
            var MinSortCode = (from a in db.TABC_DM_CostItems select a.报表顺序码).Min();
            int MySortCode = MaxSortCode.Value + 1;

            if (EditFlag.Value == "Add")
            {
                if (TextBox_SortCode.Text != "")
                {
                    MySortCode = Convert.ToInt32(TextBox_SortCode.Text);
                    if (MySortCode > MinSortCode && MySortCode < MaxSortCode)
                    {
                        SortCodeRefresh(MySortCode,1000);

                    }else                    
                    {

                        MySortCode = MaxSortCode.Value + 1;
                    }
                }
               
                string MyId = TextBox_Id.Text;
                var IsExist = from a in db.TABC_DM_CostItems where a.成本细项ID == MyId select a;

                if (IsExist.Count() == 1)
                {

                    alert = "<script>alert('该成本细项已存在');</script>";
                    return;
                }

                TABC_DM_CostItems AddRow = new TABC_DM_CostItems();
                AddRow.成本细项ID = MyId;
                AddRow.成本细项名称 = TextBox_Name.Text;
                AddRow.类型 = '0';
                AddRow.报表顺序码 = MySortCode;


                db.TABC_DM_CostItems.InsertOnSubmit(AddRow);
                try
                {
                    db.SubmitChanges();
                    alert = "<script>alert('增加成功');</script>";

                    EditState("Browse");
                    BindMyData(Convert.ToInt32(CurrentPage.Value));
                }
                catch
                {
                    alert = "<script>alert('增加失败');</script>";
                
                }
            
            }
            else if (EditFlag.Value == "Edit")
            {



                string MyId = TextBox_Id.Text;
                var EditRow = (from a in db.TABC_DM_CostItems where a.成本细项ID == MyId select a).Single();

                if (TextBox_SortCode.Text != "")
                {
                    MySortCode = Convert.ToInt32(TextBox_SortCode.Text);
                    if (MySortCode > MinSortCode && MySortCode < MaxSortCode)
                    {
                        SortCodeRefresh(MySortCode, EditRow.报表顺序码.Value);

                    }
                    else
                    {

                        MySortCode = MaxSortCode.Value;
                        SortCodeRefresh(MySortCode, EditRow.报表顺序码.Value);

                    }
                }
                else
                {
                    alert = "<script>alert('报表顺序码不能为空');</script>";
                    return;
                }

                EditRow.成本细项名称 = TextBox_Name.Text;
                EditRow.报表顺序码 = MySortCode;

                try
                {
                    db.SubmitChanges();
                    alert = "<script>alert('修改成功');</script>";

                    EditState("Browse");
                    BindMyData(Convert.ToInt32(CurrentPage.Value));
                }
                catch
                {
                    alert = "<script>alert('修改失败');</script>";

                }
            
            }
        }

        protected void Button_Cancel_Click(object sender, EventArgs e)
        {
            EditState("Browse");
        }

        protected void Button_Edit_Command(object sender, CommandEventArgs e)
        {
            string MyCostItemsId = e.CommandArgument.ToString();

            var MyQuery = from a in db.TABC_DM_CostItems where a.成本细项ID == MyCostItemsId select a;
            if(MyQuery.Count() == 1)
            {
               EditState("Edit");
     
                var MyRow = MyQuery.Single();
                TextBox_Id.Text = MyRow.成本细项ID;
                TextBox_Name.Text = MyRow.成本细项名称;
                TextBox_SortCode.Text = MyRow.报表顺序码.ToString();
            
            
            }
  
        }

        protected void Button_Del_Command(object sender, CommandEventArgs e)
        {
            string DelCostItemsId = e.CommandArgument.ToString();

            var MyQuery = from a in db.TABC_DM_CostItems where a.成本细项ID == DelCostItemsId select a;
            
            if(MyQuery.Count() == 1)
            {
                var MyRow = MyQuery.Single();

                if (MyRow.类型 == '0')
                {
                    alert = "<script>alert('系统定义的成本细项不允许删除');</script>";
                    return;
                }
                else
                {
                    db.TABC_DM_CostItems.DeleteOnSubmit(MyQuery.Single());
                    db.SubmitChanges();
                    alert = "<script>alert('删除成功');</script>";
                    BindMyData(Convert.ToInt32(CurrentPage.Value));

                }
            
            }
        }



    }
}
